
const key="userInfo"
const status = "isAuthenticated"
const defaultAuthenticatedStatus = false;
//array,obj的存储都应该序列化
//vuex

export const getUserInfo=()=>{
    const defaultUserInfo={
        id:-1,
        username:"未登录",
    }
    let userInfo = localStorage.getItem(key)
    return userInfo?JSON.parse(userInfo):defaultUserInfo;
}

export function setUserInfo(userInfo){
    let oldUserInfo = getUserInfo()
    let newUserInfo = {...oldUserInfo,...userInfo}
    localStorage.setItem(key,JSON.stringify(newUserInfo))
}
export function removeUserInfo(){
    localStorage.removeItem(key)
}

export const getAuthenticatedStatus = () => {
    let isAuthenticated = localStorage.getItem(status);
    if (isAuthenticated === null) {
      localStorage.setItem(status, defaultAuthenticatedStatus.toString());  // 默认为 false
      isAuthenticated = localStorage.getItem(status);
    }
    return isAuthenticated === "true";  // 转换为布尔值 true 或 false
  }
  
  export const switchAuthticatedStatus = () => {
    let currentStatus = localStorage.getItem(status);
  
    // 如果 currentStatus 存在，则取反
    if (currentStatus !== null) {
      const afterStatus = currentStatus === "true" ? false : true;  // 反转布尔值
      localStorage.setItem(status, afterStatus.toString());  // 存储为字符串
      return afterStatus;  // 返回布尔值
    } else {
      // 如果没有值，则设置默认值
      localStorage.setItem(status, defaultAuthenticatedStatus.toString());
      return defaultAuthenticatedStatus;  // 返回布尔值
    }
  }
  